from django.http import JsonResponse
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from aichat.myAI.geneName import generate_name
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
import random


# Create your views here.
# 起名请求
class GenerateNameView(APIView):
    def post(self, request):
        name_info = request.data
        print(name_info)
        species = name_info['species']
        trait = name_info['trait']
        nameStyle = name_info['nameStyle']
        return Response({'aiMessage': generate_name(species, trait, nameStyle), 'code': 200})


# ws测试
class WsTest(APIView):
    def get(self, request):
        # update = AllDataConsumersUpdate()
        # update.all_active_visit_data()
        channel_layer = get_channel_layer()
        send_data = {"id": random.randint(1, 100), "name": "王五"}
        async_to_sync(channel_layer.group_send)(
            '22',  # 房间组名
            {
                'type': 'send_to_chrome',  # 消费者中处理的函数
                'data': send_data
            }
        )
        return Response({"code": 200, "msg": "更新数据成功"})
    
    
# echarts 测试
class EchartsTest(APIView):
    def get(self, request):
        n1 = request.GET.get("n1")
        n2 = request.GET.get("n2")
        n3 = request.GET.get("n3")
        nlist = [n1, n2, n3]

        channel_layer = get_channel_layer()
        async_to_sync(channel_layer.group_send)(
            'home',  # 房间组名
            {
                'type': 'send_to_chrome',  # 消费者中处理的函数
                'data': nlist
            }
        )
        return Response({"code": 200, "msg": "更新数据成功"})